package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.ChangePasswordBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 14.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @dec 修改密码
 */
public class ChangePasswordActivity extends Activity {
    private static final String TAG = "ChangePasswordActivity";
    @Bind(R.id.activity_change_password_iv_return)
    ImageView activityChangePasswordIvReturn;
    @Bind(R.id.activity_change_password_origina_et_phone_number)
    EditText activityChangePasswordOriginaEtPhoneNumber;
    @Bind(R.id.activity_change_password_et_new_phone_number)
    EditText activityChangePasswordEtNewPhoneNumber;
    @Bind(R.id.activity_change_password_et_confirm_phone_number)
    EditText activityChangePasswordEtConfirmPhoneNumber;
    @Bind(R.id.activity_change_password_rl_login)
    RelativeLayout activityChangePasswordRlLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);


        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);

    }

    @OnClick({R.id.activity_change_password_iv_return, R.id.activity_change_password_rl_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_change_password_iv_return:
                finish();
                break;
            case R.id.activity_change_password_rl_login:
                String originaPhoneNumber  =  activityChangePasswordOriginaEtPhoneNumber.getText().toString().trim();
                if(!TextUtils.isEmpty(originaPhoneNumber)){
                    String newPhoneNumber  =  activityChangePasswordEtNewPhoneNumber.getText().toString().trim();
                    if(!TextUtils.isEmpty(newPhoneNumber)){
                        String confirPhoneNumber  =  activityChangePasswordEtConfirmPhoneNumber.getText().toString().trim();
                        if(!TextUtils.isEmpty(confirPhoneNumber)){
                            if(newPhoneNumber.equals(confirPhoneNumber)){
                                submitChangePassword(originaPhoneNumber,newPhoneNumber,confirPhoneNumber);
                            }else{
                                Toast.makeText(MyApplication.getMcontext(),"新密码和确认密码不一致",Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(MyApplication.getMcontext(),"请输入确认密码",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(MyApplication.getMcontext(),"请输入新密码",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(MyApplication.getMcontext(),"请输入原密码",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /***
     * 提交修改密码
     * @param originaPhoneNumber
     * @param newPhoneNumber
     * @param confirPhoneNumber
     */
    private void submitChangePassword(String originaPhoneNumber, String newPhoneNumber, String confirPhoneNumber) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        Log.e(TAG,"praiseParseNetworkResponse============>"+RBConstants.SERVER_URL+RBConstants.CHANGE_PASSWORD);
        Log.e(TAG,"praiseParseNetworkResponse====user_id========>"+user_id);
        Log.e(TAG,"praiseParseNetworkResponse====old_password========>"+originaPhoneNumber);
        Log.e(TAG,"praiseParseNetworkResponse====new_password========>"+newPhoneNumber);
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.CHANGE_PASSWORD)
                .addParams("cus_id", user_id)
                .addParams("old_password", originaPhoneNumber)
                .addParams("new_password", newPhoneNumber)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final   ChangePasswordBean changePasswordBean = gson.fromJson(praiseParseNetworkResponse,ChangePasswordBean.class);
                        if(changePasswordBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),changePasswordBean.errorMessage,Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),changePasswordBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
